package com.useeinfo.oa.modules.security.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.oa.modules.security.dao.SystemModuleDao;
import com.useeinfo.oa.modules.security.entity.SystemModule;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("SystemModuleBiz")
public class SystemModuleBiz extends CrudBiz<SystemModuleDao, SystemModule> {

    /**
     * 查询模块是否存在-通过名称
     *
     * @param systemModule
     * @return
     */
    public Boolean existSystemModuleName(SystemModule systemModule) {
        if (StringUtils.isBlank(systemModule.getModuleName())) {
            return true;
        }
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("moduleName", systemModule.getModuleName());
        List<SystemModule> systemModules = findList(queryParam);

        if (systemModule.getId() == null) {
            return CollectionUtils.isNotEmpty(systemModules);
        } else {
            return !((systemModules.size() == 1 && systemModules.get(0).getId().equals(systemModule.getId())) || systemModules.size() == 0);
        }
    }

    /**
     * 查询模块是否存在-应用id
     *
     * @param applicationModuleId
     * @return
     */
    public Boolean existSystemModuleByapplicationId(String applicationModuleId) {
        SystemModule systemModule = findSystemModuleByapplicationId(applicationModuleId);
        return systemModule != null;
    }

    /**
     * 查询模块-应用id
     *
     * @param applicationModuleId
     * @return
     */
    public SystemModule findSystemModuleByapplicationId(String applicationModuleId) {
        if (StringUtils.isBlank(applicationModuleId)) {
            return null;
        }
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("applicationModuleId", applicationModuleId);
        List<SystemModule> systemModules = this.findList(queryParam);
        if (CollectionUtils.isNotEmpty(systemModules)) {
            return systemModules.get(0);
        }
        return null;
    }
}
